/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.SeverityLevelDetailDao;
import com.rbh.risk.entity.Category;
import com.rbh.risk.entity.SeverityLevelDetail;
import com.rbh.risk.support.SessionFactorySupport;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("severityLevelDetailDao")
public class SeverityLevelDetailDaoImpl extends SessionFactorySupport implements SeverityLevelDetailDao {

    @Override
    public void insert(SeverityLevelDetail severityLevelDetail) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().save(severityLevelDetail);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void update(SeverityLevelDetail severityLevelDetail) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().update(severityLevelDetail);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(SeverityLevelDetail severityLevelDetail) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().delete(severityLevelDetail);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public SeverityLevelDetail getById(Integer severityLevelDetailId) throws DataAccessException {
        SeverityLevelDetail result = null;
        try {
            result = (SeverityLevelDetail) super.getSessionFactory().getCurrentSession().get(SeverityLevelDetail.class, severityLevelDetailId.shortValue());
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<SeverityLevelDetail> getAll() throws DataAccessException {
        List<SeverityLevelDetail> results = null;
        try {
            Query query = super.getSessionFactory().getCurrentSession().createQuery("from " + SeverityLevelDetail.class.getName());
            results = query.setCacheable(true).list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<SeverityLevelDetail> getByDefault() throws DataAccessException {
        List<SeverityLevelDetail> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(SeverityLevelDetail.class);
            criteria.add(Restrictions.isNull("category"));
            results = criteria.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<SeverityLevelDetail> getByCategory(Category category) throws DataAccessException {
        List<SeverityLevelDetail> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(SeverityLevelDetail.class);
            criteria.add(Restrictions.eq("category", category));

            if (criteria.list().isEmpty()) {
                results = this.getByDefault();
            } else {
                results = criteria.list();
            }
        } catch (HibernateException | DataAccessException ex) {
            throw ex;
        }
        return results;
    }
}
